package com.edu.threadpool.rejected.policy;

import java.util.concurrent.*;

public class ThreadPoolRejectedPolicy {

    static ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
            1,
            2,
            3,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(1),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws InterruptedException {

            for (int i = 0; i < 5; i++) {
                Thread.sleep(100);
                try {
                    Future<?> submit = poolExecutor.submit(new Task(i));
                    
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("记录异常线程");
                }
                Thread.sleep(100);
            }
        }
}

class Task implements Runnable {

    private Integer i;

    public Task(Integer i) {
        this.i = i;
    }


    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " start ");
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " end ");
        System.out.println(" task " + i + " finished");
    }
}
